﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// FPS工具
    /// 可使用GUI快速显示帧率
    /// 也可获取当前帧率
    /// </summary>
    public class FPSUtility : MonoSingleton<FPSUtility>
    {
        /// <summary>
        /// 帧率
        /// </summary>
        public int FPS { get; private set; } = 0;
        /// <summary>
        /// 显示帧率
        /// </summary>
        public bool ShowGUI { get; set; } = false;

        private float curSeconds = 0;//当前时间
        private GUIStyle fpsStyle;//guiStyle
        private int fps;//当前帧数
        private float refreshInterval;//刷新间隔
        private bool isCalc;//是否开始计算
        private void Update()
        {
            if (!isCalc) return;
            curSeconds += Time.deltaTime;
            if (curSeconds >= refreshInterval)
            {
                FPS = (int)(fps / refreshInterval);
                fps = 0;
                curSeconds = 0;
            }
            else
                fps += 1;
        }

        private void OnGUI()
        {
            if (ShowGUI)
            {
                fpsStyle = GUI.skin.GetStyle("label");
                fpsStyle.fontSize = 50;
                GUILayout.Label("FPS:" + FPS.ToString(), fpsStyle);
            }
        }

        /// <summary>
        /// 开始计算
        /// </summary>
        /// <param name="refreshInterval"></param>
        public void StartCalc(float refreshInterval)
        {
            this.refreshInterval = refreshInterval;
            isCalc = true;
            Debug.Log("开始帧率计算");
        }
        /// <summary>
        /// 停止计算
        /// </summary>
        public void StopCals()
        {
            isCalc = false;
            FPS = 0;
            Debug.Log("停止帧率计算");
        }
    }
}